<?php
class RegisterController extends Zend_Controller_Action{
	private $oUser;	// the currently logged in user
	public function init(){
		
		
	}
	
	/**
	 * 
	 * Function used to register the user with the website. 
	 * 
	  */
	
	public function indexAction(){
	
		
		$request = $this->getRequest();
		
		if($request->isPost()){
			
			// getting the user submitted values
			$username 		= $request->getPost("userName");
			$password 		= $request->getPost("reg_password");
			$screenName		= $request->getPost("screen_name");
			$mobileNumber   = $request->getPost("mobile_number");
			$fileName       = $_FILES['file_name']['name'];
			

			$destinationFilePath = APPLICATION_PATH . "/../public/files/user" ;
			$tmp_name = $_FILES["file_name"]["tmp_name"];
			
			
			
				// save data in user table
				
				$oUser = new User();
				$oUser->setUsername($username)
								->setPassword(md5($password))
								->setScreenName($screenName)
								->setMobileNumber($mobileNumber)
								->setCreatedDate(date('Y-m-d'))
								->setRoleId(3)
								->setIsActive("Y")
								->save();
				if ($fileName != "")
				{
						
						if (! file_exists ( $destinationFilePath ))
						{
							mkdir ( $destinationFilePath, 0755, TRUE ); // create the directory structure
						}
			 		
				        $path = $destinationFilePath."/".$_FILES['file_name']['name'];		   
				             	
			        		
		        	
						$oMedia = new Media();											
						$oMedia->setOwnerId($oUser->getId())
						       ->setName($screenName)
						       ->setType('user')
						       ->setFileType('image')
							   ->setFileName($fileName)
							   ->setFileSize($_FILES['file_name']['size'])
							   ->setMimeType($_FILES['file_name']['type'])
							   ->setAddedDate(date('Y-m-d'))
							   ->save();   	
					 move_uploaded_file($tmp_name,$path);			   
			    }					
				
				// hash generation and email to user for request for activation. 
					$mailDetails = array(
	
												"to"				=>	$username,
												"subject"			=>	"Your Account Activation on CraziCric",
												"name"			    =>  $screenName,
												"email"				=>  $username,
												"pwd"				=>  $password,
												
												
											);
	
						$oMail = new Default_Service_Mail();
						$oMail->sendMail("register", $mailDetails);
	
						$this->view->info = array(
	                                                    "type"		=>  "success",
	                                                    "message"	=>	Default_Service_Message::MAIL_SENT_SUCCESSFULLY
	                    );
	                  			
						// redirect
						$this->_helper->redirector("confirmation", "register");				
				   				
			
		}
	}

/**
 * 
 * Function used to activate the user account 
 */	
	public function activateAction(){
			
		
	}
	/**
	 * Function used to show the confirmation message after the register.
	 * 
	 */
	
	public function confirmationAction(){
		
	}

	
	public function doneAction(){
		
	}
}